﻿using System;
using System.ComponentModel.Composition;
using Core.Interfaces;
using Core.BusinessLogic.Interfaces;
using Core.DataAccess;
using Core.Common;

namespace SonCa.SocialNetwork.Images.ProfileAvatar
{
    public partial class ProfileImage : System.Web.UI.Page
    {
        [Import]
        private IUserSession _userSession;
        [Import]
        private IAccountService _accountService;
        [Import]
        private IWebContext _webContext;

        private Int32 accountID;
        private Account account;
        private Profile profile;

        protected void Page_Load(object sender, EventArgs e)
        {
            MEFManager.Compose(this);
            if (_webContext.AccountID > 0)
            {
                accountID = _webContext.AccountID;                
                account = _accountService.GetAccountByID(accountID);
            }
            else
            {
                if (_userSession.IsLogged  && _userSession.CurrentUser != null)
                {
                    account = _userSession.CurrentUser;
                }
            }
            if (account == null)
            {
                Response.Redirect("~/Images/ProfileAvatar/NoPhotoAvailable.png");
            }

            profile = account.Profile;
            if (profile != null && profile.Avatar != null)
            {
                Response.Clear();
                Response.ContentType = profile.AvatarMimeType;
                Response.BinaryWrite(profile.Avatar);
            }
            else
            {
                Response.Redirect("~/Images/ProfileAvatar/NoPhotoAvailable.png");
            }
        }
    }
}